#include "widgetboxcategorylistview.h"

#include "widgetboxcategorymodel.h"

WidgetBoxCategoryListView::WidgetBoxCategoryListView(QWidget *parent):
    QListView(parent),
    m_model(new WidgetBoxCategoryModel(this))
{
    setFocusPolicy(Qt::NoFocus);
    setFrameShape(QFrame::NoFrame);
    setIconSize(QSize(22, 22));
    setSpacing(1);
    setTextElideMode(Qt::ElideMiddle);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setResizeMode(QListView::Adjust);
    setUniformItemSizes(true);

    connect(this, SIGNAL(pressed(QModelIndex)), this, SLOT(slotPressed(QModelIndex)));
    setEditTriggers(QAbstractItemView::AnyKeyPressed);
    setModel(m_model);
}

void WidgetBoxCategoryListView::setViewMode(ViewMode vm)
{
    QListView::setViewMode(vm);
    m_model->setViewMode(vm);
}

void WidgetBoxCategoryListView::setCurrentItem(int row)
{
    const QModelIndex index = m_model->index(row,0);
    if (index.isValid())
        setCurrentIndex(index);
}

void WidgetBoxCategoryListView::slotPressed(const QModelIndex &index)
{
    Widget wgt = m_model->widgetAt(index);
    if (wgt.isNull())
        return;
    emit pressed(wgt.pluginName(),wgt.className(), QCursor::pos());
}

int WidgetBoxCategoryListView::count() const
{
    return m_model->rowCount();
}

Widget WidgetBoxCategoryListView::widgetAt(const QModelIndex & index) const
{
    return m_model->widgetAt(index);
}

Widget WidgetBoxCategoryListView::widgetAt( int row) const
{
    return m_model->widgetAt(row);
}

bool WidgetBoxCategoryListView::containsWidget(const QString &name)
{
    return m_model->indexOfWidget(name) != -1;
}

void WidgetBoxCategoryListView::addWidget(const Widget &widget, const QIcon &icon, bool editable)
{
    m_model->addWidget(widget, icon, editable);
}

Category WidgetBoxCategoryListView::category() const
{
    return m_model->category();
}
